package com.cw.controller;

import com.cw.beans.User;
import com.cw.mapper.UserMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class LogingController {
    @Autowired
    private UserMapper userMapper;
//登录方法
@RequestMapping("/login")
    public String login (Model model,String username, String password , HttpServletRequest request){
    HttpSession  session = request.getSession();
        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);

        User user = userMapper.getUser(user1);

        if (user == null) {
            model.addAttribute("msg", "登录失败请重新输入");
            return "index";
        } else if (user != null) {
            session.setAttribute("nowUser", user);
            if (user.getRid() == 1) {
                model.addAttribute("user", user);
                model.addAttribute("mag", "管理员登录成功");
                session.setAttribute("nowUser",user);
                //管理员的方法
                return "home";
            } else if (user.getRid() == 2) {
                //财务
                return "financial";
            } else if (user.getRid() == 3) {
                //部门
                return "dept";
            } else if (user.getRid() == 4) {
                //运维
                return "operations";
            }
        }
        model.addAttribute("mag", "登录失败请重新输入");
        //错误代码
        return "index";

    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
}